package servlets.invitation;

import hibernate.method.InvitationMethods;
import hibernate.method.UserMethods;
import hibernate.model.Invitation;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Servlet implementation class InvitationDetails
 */
@WebServlet("/InvitationDetails")
public class InvitationDetails extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InvitationDetails() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user = request.getParameter("user");
		int userID = Integer.parseInt(user);
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		String typeOfReq= request.getParameter("type");
		response.setContentType("text/plain");
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session s = sessionFactory.openSession();
		s.beginTransaction();
		UserMethods u_m = new UserMethods();
		InvitationMethods i_m = new InvitationMethods();
		//User u = u_m.id_to_user(sessionFactory, s, userID);
		List <Invitation> inv = new ArrayList<Invitation>();
		if ("otrzymane".equals(typeOfReq)){
			inv = i_m.listActiveInvitationsByReceiver(sessionFactory, s, userID);
		}
		if("wyslane".equals(typeOfReq)){
			inv = i_m.listActiveInvitationsBySender(sessionFactory, s, userID);
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append("<table>");
		sb.append("<tr> <td> Lp </td> <td> Nazwa uzytkownika </td> <td> Akcja </td> </tr>");
		int counter = 1;
		for (Invitation i :inv){
			sb.append("<tr> <td>");
			sb.append(counter);
			sb.append("</td><td>");
			sb.append("<a class=\"inv_user\" id=\""+ i.getInviting() + "\" href=\"#\">" + u_m.id_to_user(sessionFactory, s, i.getInviting()).getUsername() + "</a>");
			sb.append("</td><td>");
			sb.append("<input type=\"radio\" name=\"check\" value=\"" +i.getInvitation_id() +"\"");
			sb.append("</td></tr>");
			counter++;
		}
		
		
		
		JSONObject json = new JSONObject();
		PrintWriter out = response.getWriter();
		try {
			json.put("text", sb.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		out.println(json);
		out.flush();

		s.getTransaction().commit();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
